function view_manual_event(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/show_living_event',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Manual Events: Live/UpComing' ,
				 zIndex: 1005
			}
		);
	});
}

function view_manual_event_closed(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/view_manual_event_closed',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Manual Events: Closed' ,
				 zIndex: 1005
			}
		);
	});
}

function view_manual_event_cancelled(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/view_manual_event_cancelled',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Manual Events: Cancelled' ,
				 zIndex: 1005
			}
		);
	});
}

function view_manual_event_hidden(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/view_manual_event_hidden',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'View Manual Events: Hidden Categories' ,
				 zIndex: 1005
			}
		);
	});
}

function add_category_manual(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/add_category',{tournament_id:tournament_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Add Manual Category' ,
				 zIndex: 1015
			}
		);
	});
}

function manual_category_edit(cat_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/edit_category',{cat_id:cat_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Edit Manual Category' ,
				 zIndex: 1015
			}
		);
	});
}

function manual_category_set_answer(cat_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/manual_category_set_answer',{cat_id:cat_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Set correct answer of category' ,
				 zIndex: 1015
			}
		);
	});
}

function change_manual_category_open_hide(cat_id){
	jQuery('#loader_image_'+cat_id).show();
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/change_manual_category_open_hide',{cat_id:cat_id},function(res){
		jQuery('#loader_image_'+cat_id).hide();
		
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_manual_event(res);//tournament_id
		}else{
			view_manual_event_hidden(res);
		}	
	});
}

function open_window_manual_event(cat_id){
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/manual_event_manage',{cat_id:cat_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 1020,
				 resizable: false,
				 height:550,
				 title: 'Betting Answer Management' ,
				 zIndex: 1015
			}
		);
	});
}

function set_bet_manual_event_result(bet_id, result){
	jQuery('#loader_image_set_result_'+bet_id).show();
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/set_bet_manual_event_result',{bet_id:bet_id, result:result},function(res){
		jQuery('#loader_image_set_result_'+bet_id).hide();
		 
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_manual_event(res);//tournament_id
		}else{
			view_manual_event_hidden(res);
		}	
	});
}


function bet_manual_event_finish(bet_id){
	jQuery('#loader_image_manage_'+bet_id).show();
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/bet_manual_event_finish',{bet_id:bet_id},function(res){
		jQuery('#loader_image_manage_'+bet_id).hide();
		if(!isIntNumber(res)){
			alert(res);
			return false;
		}
		open_window_manual_event(res);//tournament_id
	});
}

function bet_manual_event_cancel(bet_id){	
	if(confirm("Are you sure want to cancel this event?")){
		jQuery('#loader_image_manage_'+bet_id).show();
		jQuery.post(BASE_URI+'admin/dataman/manual_tournament/bet_manual_event_cancel',{bet_id:bet_id},function(res){
			jQuery('#loader_image_manage_'+bet_id).hide();
			if(!isIntNumber(res)){
				alert(res);
				return false;
			}
			open_window_manual_event(res);//tournament_id
		});
	}
}

function manual_category_cancel(cat_id){	
	if(confirm("Are you sure want to cancel this category?")){
		jQuery('#loader_image_manual_'+cat_id).show();
		jQuery.post(BASE_URI+'admin/dataman/manual_tournament/manual_category_cancel',{cat_id:cat_id},function(res){
			jQuery('#loader_image_manual_'+cat_id).hide();
			if(!isIntNumber(res)){
				alert(res);
				return false;
			}
			 
			$mode = jQuery('#dialog_mode').val();
			if($mode == 'open'){
				view_manual_event(res);//tournament_id
			}else{
				view_manual_event_hidden(res);
			}	
		});
	}
}

function manual_category_finish(cat_id){
	jQuery('#loader_image_manual_'+cat_id).show();
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/manual_category_finish',{cat_id:cat_id},function(res){
		jQuery('#loader_image_manual_'+cat_id).hide();
		if(!isIntNumber(res)){
			alert(res);
			return false;
		}
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_manual_event(res);//tournament_id
		}else{
			view_manual_event_hidden(res);
		}	
	});
}

function apply_answer_to_category(cat_id){
	jQuery('#loader_image_apply_answer').show();
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/apply_answer_to_category',{cat_id:cat_id},function(res){
		jQuery('#loader_image_apply_answer').hide();
		if(!isIntNumber(res)){
			alert(res);
			return false;
		}
		open_window_manual_event(cat_id);//cat_id
	});
}

function apply_answer_to_all_category_of_match(tournament_id){
	jQuery('#loader_image_apply_answer_to_all_cats').show();
	jQuery.post(BASE_URI+'admin/dataman/manual_tournament/apply_answer_to_all_category_of_match',{tournament_id:tournament_id},function(res){
		jQuery('#loader_image_apply_answer_to_all_cats').hide();
		alert(res);
		 
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_manual_event(tournament_id);//tournament_id
		}else{
			view_manual_event_hidden(tournament_id);
		}	
	});
}

function changeTypeCategory(value){
	jQuery('#box_div, #range_div').hide();
	jQuery('#'+value+'_div').show();
}



jQuery(document).ready(function() {
	var optionsAddCategory = { 
		beforeSubmit:beforeSubmitAddCategory,   
		success:afterSubmitAddCategory
	};	
	jQuery("#add_new_category_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsAddCategory);
		return false;
	});	 
	jQuery("#edit_category_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsAddCategory);
		return false;
	});	 
	jQuery("#manual_category_set_answer_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsAddCategory);
		return false;
	});	 
});		

function beforeSubmitAddCategory(formData, jqForm, options){
	jQuery('#loader_image').show();
	jQuery.data(document.body, 'tournament_id', jQuery('#tournament_id').val());
	return true;
}

function afterSubmitAddCategory(responseText, statusText, xhr, $form){
	jQuery('#loader').hide();	
	if(responseText == 'ok'){
		 
		$mode = jQuery('#dialog_mode').val();
		if($mode == 'open'){
			view_manual_event(jQuery.data(document.body, 'tournament_id'));//tournament_id
		}else{
			view_manual_event_hidden(jQuery.data(document.body, 'tournament_id'));
		}	
		jQuery('#dialog2').dialog('close');
	}else{
		alert(responseText);
	}
}







